/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rpg.controller;

import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import rpg.Dungeon;
import rpg.PlayerCharacter;
import rpg.gameSkeleton.BattleObserver;

/**
 *
 * @author Emmanouil Samatas
 */
public class HeroCollisionManager {

    private int dx, dy;
    PlayerCharacter playerCharacter;
    Dungeon dungeon;
    List<BattleObserver> observerList = new ArrayList<>();

    public HeroCollisionManager(PlayerCharacter myHero, Dungeon dungeonMap) {
        this.dungeon = dungeonMap;
        this.playerCharacter = myHero;
        setInitialPosition();
    }

    public void registerObserver(BattleObserver listener) {
        observerList.add(listener);
    }

    public void notifyListener() {
        for (BattleObserver bo : observerList) {
            bo.battleBegins();
        }
    }

    public void keyPressed(KeyEvent e) {


        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
            int x = playerCharacter.getX();
            int y = playerCharacter.getY();

            if (x - 1 < 0) {
                dx = 0;
            } else if(dungeon.getMapPoint(x - 1, y).equals("w")){
                dx = 0;
            } else if(dungeon.getMapPoint(x - 1, y).equals("e")){
                dx = -1;
                notifyListener();
            } else {
                dx = -1;
            }

            playerCharacter.setDx(dx);

        }

        if (key == KeyEvent.VK_RIGHT) {
            int x = playerCharacter.getX();
            int y = playerCharacter.getY();



            if (x + 1 > dungeon.getMap().get(y).length() - 1) {

                dx = 0;
            } else if (dungeon.getMapPoint(x + 1, y).equals("w")) {
                dx = 0;
            } else if (dungeon.getMapPoint(x + 1, y).equals("e")) {
                dx = 1;
                notifyListener();

            } else {
                dx = 1;
            }




            playerCharacter.setDx(dx);

        }

        if (key == KeyEvent.VK_UP) {
            int x = playerCharacter.getX();
            int y = playerCharacter.getY();

            if (y - 1 < 0) {
                dy = 0;
            } else if (dungeon.getMapPoint(x, y - 1).equals("w")) {
                dy = 0;
            } else if (dungeon.getMapPoint(x, y - 1).equals("e")) {
                dy = -1;
                notifyListener();
            } else {
                dy = -1;
            }




            playerCharacter.setDy(dy);

        }

        if (key == KeyEvent.VK_DOWN) {
            int x = playerCharacter.getX();
            int y = playerCharacter.getY();

            if (y + 1 >= dungeon.getMap().size()) {
                dy = 0;
            } else if (dungeon.getMapPoint(x, y + 1).equals("w")) {
                dy = 0;
            } else if (dungeon.getMapPoint(x, y + 1).equals("e")) {
                dy = 1;
                notifyListener();
            } else {
                dy = 1;
            }


            playerCharacter.setDy(dy);
        }

    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
            playerCharacter.setDx(dx);
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
            playerCharacter.setDx(dx);
        }

        if (key == KeyEvent.VK_UP) {
            dy = 0;
            playerCharacter.setDy(dy);
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
            playerCharacter.setDy(dy);
        }
    }

    private void setInitialPosition() {
        playerCharacter.setX(dungeon.getPlayerPoint()[0]);
        playerCharacter.setY(dungeon.getPlayerPoint()[1]);

        int x = dungeon.getPlayerPoint()[0];
        int y = dungeon.getPlayerPoint()[1];
        playerCharacter.setCurrentPosition(x, y);
    }
}
